<?php

namespace app\h5api\validate;

use think\Validate;

class Reserve extends Validate
{

    protected $scene = [
        'checkReserve' => ['place_time','sex','reserve_uname','reserve_uphone','shop_id','persons'],
    ];

    protected $rule =   [
        'place_time'  => 'require|dateFormat:Y-m-d H:i|checkTime',
        'sex'  => 'require|checkSex',
        'persons'  => 'require|number',
        'reserve_uname' => 'require|chsAlphaNum',
        'reserve_uphone' => 'require|mobile',
        'shop_id'  => 'require|number',
    ];

    protected $message  =   [
        'shop_id.require' => '店铺参数必须',
        'shop_id.number'   => '店铺参数有误',
        'place_time.require' => '预约用餐时间必须',
        'place_time.dateFormat:Y-m-d H:i'   => '预约时间有误',
        'sex.require' => '用户性别必须',
        'persons.require' => '用餐人数必须',
        'persons.number'   => '人数必须是数字',
        'reserve_uname.require' => '预约人名必须',
        'reserve_uname.chsAlphaNum'   => '人名有误',
        'reserve_uphone.require' => '预约联系电话必须',
        'reserve_uphone.mobile'   => '电话格式有误',
    ];

    protected function checkSex($value)
    {
        if($value != 1 && $value !=2){
            return '性别参数有误';
        }
        return true;
    }

    protected function checkTime($value)
    {
        if(!empty($value)){
//            if(date('H:i',$value) < date('H:i',time())){
            if($value < date('Y-m-d H:i',time())){
                return '该时间段已暂停预约';
            }
        }
        return true;
    }

}
